﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using System.Configuration;
using System.Web;
using Project.Core.Model;

namespace Project.Core.DAL
{
    public class MongoDBFactory
    {
        private MongoClient Client { get; set; }
        
        public MongoDBFactory()
        {
            Client = new MongoClient(MongoDBConfig.ConnectionString);
        }

        public IMongoDatabase Database { get => Client.GetDatabase(new MongoUrl(MongoDBConfig.ConnectionString).AuthenticationSource); }

        public IMongoCollection<T> DbSet<T>() where T : BaseModel => Database.GetCollection<T>("MengTLog.Logger");

        //public static string MonStr = ConfigurationManager.ConnectionStrings["jydb"].ConnectionString;
        //private static string DataBase = System.Web.Configuration.WebConfigurationManager.AppSettings["JYDatabase"];
        //private static IMongoDatabase dbAsync = null;
        //private static MongoDatabase db = null;
        //public static IMongoDatabase GetAsyncMongoDataBase()
        //{
        //    var MongoClient = new MongoClient(MonStr);

        //    //MongoServer Server = MongoDB.Driver.MongoServer.Create(MonStr);
        //    if (dbAsync == null)
        //    {
        //        //dbAsync = MongoClient.GetDatabase(DataBase);
        //        dbAsync = MongoClient.GetDatabase(new MongoUrl(MonStr).AuthenticationSource);
        //        //CallContext.SetData("MongoDataBase", dbAsync);
        //    }
        //    return dbAsync;
        //}

        //public static MongoDatabase GetMongoDataBase()
        //{
        //    var MongoClient = new MongoClient(MonStr);
        //    var Server = MongoClient.GetServer();
        //    //db = CallContext.GetData("MongoDataBase") as MongoDatabase;
        //    if (db == null)
        //    {
        //        //db = Server.GetDatabase(DataBase);
        //        db = Server.GetDatabase(new MongoUrl(MonStr).AuthenticationSource);
        //        //CallContext.SetData("MongoDataBase", db);
        //    }
        //    return db;
        //}
    }
}
